﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Collections;

namespace Acsys.Entity
{
    public class Vessel : IEntity
    {
        public int ID = 0;
        public string CODE = "VESS-";
        public string NAME = "";
        public Owner OWNER= null;

        public Vessel()
        {
        }
        public Vessel(int id)
        {
            ID = id;
        }
        public Vessel(int id, string code)
        {
            ID = id;
            CODE = code;
        }
        public IEntity Get(MySqlDataReader aReader)
        {
            Vessel ves = null;
            while (aReader.Read())
            {
                ves = new Vessel();
                ves.ID = Convert.ToInt32(aReader[0]);
                ves.CODE = aReader[1].ToString();
                ves.NAME = aReader[2].ToString();
                ves.OWNER = Convert.ToInt32(aReader[3]) == 0 ? null : new Owner(Convert.ToInt32(aReader[3]));
            }
            aReader.Close();
            return ves;
        }
        public string GetInsertSQL()
        {
            return String.Format(@"insert into table_vessel 
                (ves_code,ves_name, owner_id) 
                VALUES ('{0}','{1}',{2})",
                CODE, NAME, OWNER == null ? 0 : OWNER.ID);
        }
        public string GetDeleteSQL()
        {
            return "delete from table_vessel where ves_id = " + ID;
        }
        public string GetUpdateSQL()
        {
            return String.Format(@"update table_vessel set 
                ves_code = '{0}', 
                ves_name='{1}',
                owner_id = {2}
                where ves_id = {3}",
                CODE, NAME, OWNER == null ? 0 : OWNER.ID, ID);
        }
        public string GetByIDSQL(int ID)
        {
            return String.Format("select * from table_vessel where ves_id = {0}", ID);
        }
        public string GetByCodeSQL(string code)
        {
            return String.Format("select * from table_vessel where ves_code = '{0}'", code);
        }
        public string GetByCodeLikeSQL(string text)
        {
            return String.Format("select * from table_vessel where ves_code like '%{0}%'", text);
        }
        public string GetByNameLikeSQL(string text)
        {
            return String.Format("select * from table_vessel where ves_name like '%{0}%'", text);
        }
        public string GetAllSQL()
        {
            return String.Format("select * from table_vessel");
        }
        public IList GetAll(MySqlDataReader aReader)
        {
            IList result = new ArrayList();
            while (aReader.Read())
            {
                Vessel ves = new Vessel();
                ves.ID = Convert.ToInt32(aReader[0]);
                ves.CODE = aReader[1].ToString();
                ves.NAME = aReader[2].ToString();
                ves.OWNER = Convert.ToInt32(aReader[3]) == 0 ? null : new Owner(Convert.ToInt32(aReader[3]));
                result.Add(ves);
            }
            aReader.Close();
            return result;
        }
        public int GetID()
        {
            return ID;
        }
        public void SetID(int id)
        {
            ID = id;
        }
        public override string ToString()
        {
            return CODE + " - " + NAME;
        }
        public string GetCode()
        {
            return CODE;
        }
        public void SetCode(string code)
        {
            CODE = code;
        }
        public string GetMaximumIDSQL()
        {
            return String.Format("SELECT max(ves_id) from table_vessel");
        }
    }
}
